Listing all Unums

The End of Error, §4.10 "A complete exact unum set for a small utag" lists all the (2,2) unums. We can recreate this


In [1]:
using Unums

In [2]:
# create a vector of all exact unums with positive sign bit
E = Array(Unum22,0)
for es in 1:2^2
    for fs in 1:2^2
        for e = 0:(2^es-1)
            for f = 0:(2^fs-1)
                u = Unum22(false,e,f,false,es,fs) # set s and u bits to false
                push!(E,u)
            end
        end
    end
end
E


Out[2]:
900-element Array{Unums.Unum{2,2,UInt16},1}:
   0.0
   1.0
   2.0
   3.0
   0.0
   0.5
   1.0
   1.5
   2.0
   2.5
   3.0
   3.5
   0.0
   ⋮  
 320.0
 336.0
 352.0
 368.0
 384.0
 400.0
 416.0
 432.0
 448.0
 464.0
 480.0
 Inf  

In [3]:
# All exact 2,2 unums can be represented by a Float64
F = [convert(Float64,x) for x in E]
Fu = unique(F)
sort!(Fu)


Out[3]:
256-element Array{Any,1}:
   0.0        
   0.000976563
   0.00195313 
   0.00292969 
   0.00390625 
   0.00488281 
   0.00585938 
   0.00683594 
   0.0078125  
   0.00878906 
   0.00976563 
   0.0107422  
   0.0117188  
   ⋮          
 320.0        
 336.0        
 352.0        
 368.0        
 384.0        
 400.0        
 416.0        
 432.0        
 448.0        
 464.0        
 480.0        
 Inf          

In [4]:
R = [convert(Rational{Int},x) for x in E]
Ru = unique(R)
sort!(Ru)


Out[4]:
256-element Array{Any,1}:
   0//1   
   1//1024
   1//512 
   3//1024
   1//256 
   5//1024
   3//512 
   7//1024
   1//128 
   9//1024
   5//512 
  11//1024
   3//256 
    ⋮     
 320//1   
 336//1   
 352//1   
 368//1   
 384//1   
 400//1   
 416//1   
 432//1   
 448//1   
 464//1   
 480//1   
   1//0   

In [5]:
# number of zeros
sum(F .== 0)


Out[5]:
16

In [ ]: